import time, pytest, sys
from appium import webdriver
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.common.by import By as by
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


class TestDemo:
    def setup(self):
        print('项目初始化')
        # appium服务监听地址
        server = 'http://localhost:4723/wd/hub'
        # app启动参数
        desired_caps = {
            "platformName": "Android",#运行平台
            "deviceName": "OnePlus",#手机名称
            "appPackage": "com.tencent.mm",#app报名
            "appActivity": "com.tencent.mm/.MainActivity",#启动界面
            "noReset": "False"#不刷新
        }

        # 驱动appium服务，使其打开测试app
        self.driver = webdriver.Remote(server, desired_caps)
        # 设置隐式等待时间
        WebDriverWait(self.driver, 30)

    def teardown(self):
        print('内存回收')
        # 退出app
        self.driver.quit()

    # 短消息测试
    def test_01(self):
        self.driver.find_element()
        print("正在运行：%s测试用例！" % sys._getframe().f_code.co_name)
        # 点击消息
        self.driver.find_element(by.XPATH, '//TextView[@text="消息"]').click()
        # 遍历消息列表
        message_list = self.driver.find_elements(by.CLASS_NAME, '//LinearLayout')
        # 如果没有消息项就主动退出该测试
        if len(message_list) == 0:
            print('暂时没有要回复的消息')
            assert 0
        # 双击消息
        for message_view in message_list:
            if message_view is not None:
                # 如果消息控件不为空就双击消息
                TouchAction(self.driver).press(el=None, x=message_view.location['x'], y=message_view['y'], pressure=0.25).release().perform().wait(110).press(el=None, x=message_view['x'], y=message_view['y'], pressure=0.25).release().perform()
                # 输入消息
                self.driver.find_element(by.XPATH, '//EditText[@text="请输入消息"]').send_keys("你好，发送消息测试")
                # 点击发送
                self.driver.find_element(by.XPATH, '//Button[@text="发送"]').click()
                # 点击返回
                self.driver.find_element(by.XPATH, '//ImageButton[@desc="返回"]').click()
                # 延迟两秒钟
                time.sleep(2)

    # 新建群组测试
    def test_02(self):
        print("正在运行：%s" % sys._getframe().f_code.co_name)
        # 点击群组
        self.driver.find_element(by.XPATH, '//TextView[@text="群组"]').click()
        # 点击新建
        self.driver.find_element(by.XPATH, '//Button[@text="新建"]').click()
        # 输入群组名称,test1
        self.driver.find_element(by.XPATH, '//EditText[@text="请输入群组名称"]').send_keys('test1')
        # 遍历左侧成员列表
        user_list = self.driver.find_elements(by.CLASS_NAME, 'LinearLayout')
        # 如果成员个数为0就退出该测试用例
        if len(user_list) == 0:
            assert 0
        for user_view in user_list:
            # 如果成员不为空，就双击该成员添加到群组
            if user_view is not None:
                TouchAction(self.driver).press(el=None, x=user_view.location['x'], y=user_view['y'], pressure=0.25).release().perform().wait(110).press(el=None, x=user_view['x'], y=user_view['y'], pressure=0.25).release().perform()
        # 点击应用
        self.driver.find_element(by.XPATH, '//TextView[@text="应用"]').click()

    # 群组功能测试
    def test_03(self):
        print("正在运行：%s" % sys._getframe().f_code.co_name)
        # 双击群组
        group_view = self.driver.find_element(by.XPATH, '//LinearLayout[@text="test1"]')
        TouchAction(self.driver).press(el=None, x=group_view.location['x'], y=group_view['y'],
                                       pressure=0.25).release().perform().wait(110).press(el=None, x=group_view['x'],
                                                                                          y=group_view['y'],
                                                                                          pressure=0.25).release().perform()
        # 获取群名称
        group_name = self.driver.find_element(by.ID, 'title')
        # 如果群名称为空就退出该测试用例
        if group_name == '':
            assert 0
        # 点击群组图标
        self.driver.find_element(by.XPATH, '//ImageButton[@desc="群组"]').click()
        # 获取群成员数量
        group_users = self.driver.find_elements(by.CLASS_NAME, 'LinearLayout')
        # 如果群成员数量小于等于2，就退出该测试用例
        if len(group_users) <= 2:
            assert 0

    # 联系人测试
    def test_04 (self):
        print("正在运行：%s" % sys._getframe().f_code.co_name)
        # 点击联系人
        self.driver.find_element(by.XPATH, '//TextView[@text="联系人"]').click()
        # 获取联系人列表
        user_list = self.driver.find_element(by.XPATH, '//LinearLayout')
        # 如果联系人个数为0就退出该测试用例
        if len(user_list) == 0:
            assert 0
        # 开始遍历所有联系人
        for user_view in user_list:
            # 如果联系人控件不为空,就双击该联系人
            if user_view is not None:
                TouchAction(self.driver).press(el=None, x=user_view.location['x'], y=user_view['y'], pressure=0.25).release().perform().wait(110).press(el=None, x=user_view['x'], y=user_view['y'], pressure=0.25).release().perform()
                # 输入消息
                self.driver.find_element(by.XPATH, '//EditText[@text="请输入消息"]').send_keys("你好，发送消息测试")
                # 点击发送
                self.driver.find_element(by.XPATH, '//Button[@text="发送"]').click()
                # 点击返回
                self.driver.find_element(by.XPATH, '//ImageButton[@desc="返回"]').click()
                # 延迟两秒钟
                time.sleep(2)


if __name__ == '__main__':
    pytest.main(['-s', 'main.py'])
